/**
 * 
 */
package io.gitee.falllee.dao.core.table;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.gitee.falllee.dao.model.BaseDaoConfig;
import io.gitee.falllee.dao.util.DaoUtils;
import io.gitee.falllee.dao.util.FilterPlus;
import io.gitee.falllee.dao.util.SortPlus;

/**
 * @author batty
 * @date 2022-06-28
 *
 */
public class SelectGroupBuilder extends FilterBuilder {
    
    private SelectBuilder selectBuilder;
    
    private String field;
    
    private String group;
    
    private String sort;
    
    private Long page = 0L;
    
    private Long rows = 0L;
    
    private List<String> params;
    
    SelectGroupBuilder(SelectBuilder parent, BaseDaoConfig daoConfig, FilterPlus filterPlus, String field,
            String group) {
        super(daoConfig, filterPlus);
        this.selectBuilder = parent;
        this.field = field;
        this.group = group;
    }
    
    public SelectGroupBuilder sort(SortPlus sortPlus) {
        this.sort = sortPlus.getSort();
        return this;
    }
    
    public SelectGroupBuilder sort(String sort) {
        this.sort = sort;
        return this;
    }
    
    /**
     * page默认0，查询所有
     * 
     * @param page
     * @return
     */
    public SelectGroupBuilder page(Long page) {
        this.page = page;
        return this;
    }
    
    /**
     * rows默认0，查询所有
     * 
     * @param rows
     * @return
     */
    public SelectGroupBuilder rows(Long rows) {
        this.rows = rows;
        return this;
    }
    
    public SelectGroupBuilder addChildrenTable(String tableName) {
        if (params == null) {
            params = new ArrayList<>();
        }
        params.add(tableName);
        return this;
    }
    
    public List<Map<String, Object>> select() {
        String[] pars = DaoUtils.list2Array(params);
        return selectBuilder.selectGroup(filter, field, group, sort, page, rows, pars);
    }
}
